﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Common.UI
{
    public partial class SplashForm : Form
    {
        public SplashForm()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            // Draw the main image.
            using (Image backgroundImage = new Bitmap(CommonResources.Splash))
            {
                // Fill the background.
                e.Graphics.Clear(Color.Black);

                // Draw the image, but scale it.
                // SourceRect is entire image.
                var srcRect = new Rectangle(0, 0, backgroundImage.Width, backgroundImage.Height);

                // Set destination rectangle to be 50 percent of the clipping rectangle.
                var destRect = new Rectangle(0, 0, e.ClipRectangle.Width / 2, e.ClipRectangle.Height / 2);

                // Reposition origin to center the image on the screen.
                destRect.Location = new Point(
                    (e.ClipRectangle.Width - destRect.Width) / 2,
                    (e.ClipRectangle.Height - destRect.Height) / 2);

                // Draw the image.
                e.Graphics.DrawImage(backgroundImage, destRect, srcRect, GraphicsUnit.Pixel);
            }
            // Draw text positioned inside a rectangle.
            var s = "Mazeball";

            // Set pen and font size.
            var penSize = 4;
            var fontSize = 10;

            using (var pen = new Pen(Color.Yellow, penSize))
            {
                using (var font = new Font("Arial", fontSize, FontStyle.Regular))
                {
                    using (var brush = new SolidBrush(Color.White))
                    {
                        var textSize = e.Graphics.MeasureString(s, font);

                        // Create a rectangle with padding space between string
                        // and box.
                        var rectWidth = Convert.ToInt32((textSize.Width) + 10);
                        var rectHeight = Convert.ToInt32((textSize.Height) + 10);
                        var r = new Rectangle(
                            (e.ClipRectangle.Width - rectWidth) / 2,
                            //e.ClipRectangle.Height - rectHeight - 15,
                            15,
                            rectWidth,
                            rectHeight);

                        e.Graphics.DrawRectangle(pen, r);
                        e.Graphics.DrawString(s, font, brush,
                            r.Left + 5, r.Top + 5);
                    }
                }
            }

            using (var font = new Font("Arial", fontSize, FontStyle.Regular))
            {
                using (var brush = new SolidBrush(Color.White))
                {
                    // Now draw a long string, but use a formatting rectangle and
                    // a StringFormat object to wrap and align the text.
                    var authors = "Authors: A really lazy dev";

                    // Define the destination rectangle.
                    var layoutRectangle = new RectangleF(15, Height - 50, Width - 20, 100);

                    // Create a StringFormat and set formatting flags.
                    var strFmt = new StringFormat
                                     {
                                         Alignment = StringAlignment.Center,
                                         FormatFlags = StringFormatFlags.NoClip
                                     };

                    // Draw the string.
                    e.Graphics.DrawString(authors, font, brush, layoutRectangle, strFmt);
                }
            }
        }
    }
}